
#include <BRect>
#include <BVector>
#include "StringUtils.h"

BString StringOfValue(const BValue& value)
{
	BString text;
	if (value.empty())
	{
		text = "none";
	}
	else if (value.is<bool>())
	{
		bool b = value;
		text << (b ? "true" : "false");
	}
	else if (value.is<char>())
	{
		char c = value;
		text << (int)c << 'c';
	}
	else if (value.is<unsigned char>())
	{
		unsigned char b = value;
		text << (int)b << 'b';
	}
	else if (value.is<short>())
	{
		short s = value;
		text << (int)s << 's';
	}
	else if (value.is<unsigned short>())
	{
		unsigned short us = value;
		text << (int)us << "us";
	}
	else if (value.is<int>())
	{
		int i = value;
		text << i;
	}
	else if (value.is<unsigned int>())
	{
		unsigned int ui = value;
		text << ui << 'u';
	}
	else if (value.is<long long>())
	{
		long long l = value;
		text << l << "l";
	}
	else if (value.is<unsigned long long>())
	{
		unsigned long long ul = value;
		text << ul << "ul";
	}
	else if (value.is<float>())
	{
		float f = value;
		text << f << 'f';
	}
	else if (value.is<double>())
	{
		double d = value;
		text << d;
	}
	else if (value.is<BPoint>())
	{
		const BPoint& point = value;
		text << point.x() << ", " << point.y();
	}
	else if (value.is<BSize>())
	{
		const BSize& size = value;
		text << size.width() << ", " << size.height();
	}
	else if (value.is<BRect>())
	{
		const BRect& rect = value;
		text << rect.x() << ", " << rect.y() << ", " << rect.width() << ", " << rect.height();
	}
	else if (value.is<BVector>())
	{
		const BVector& vec = value;
		text << vec.x() << ", " << vec.y() << ", " << vec.z();
	}
	else if (value.is<BString>())
	{
		const BString& str = value;
		text << '\"' << str << '\"';
	}
	else if (const BStringList* path = value)
	{
		for (int i = 0; i < path->size(); i++)
			text << path->at(i) << '.';
		text -= '.';
	}
	return text;
}
BValue StringToValue(const BString& str)
{
	BValue value;
	if (str.beginWith('\"'))
	{
		value = str;
		BString& text = value;
		text.remove(0);
		text -= '\"';
	}
	else if (str == "true")
	{
		value = true;
	}
	else if (str == "false")
	{
		value = false;
	}
	else if (str.isNumber())
	{
		if (str.contain('.'))
		{
			if (str.endWith('f'))
			{
				float v = str.toFloat();
				value = v;
			}
			else
			{
				double v = str.toDouble();
				value = v;
			}
		}
		else
		{
			if (str.endWith("ul"))
			{
				unsigned long long v = str.toLong();
				value = v;
			}
			else if (str.endWith('l'))
			{
				long long v = str.toLong();
				value = v;
			}
			else
			{
				int v = str.toInt();
				value = v;
			}
		}
	}
	else
	{
		BStringArray& path = value.fill<BStringArray>();
		str.split(path, '.');
	}
	return value;
}

